package com.qdlc.p2p.biz.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.qdlc.p2p.biz.service.AdditionalCommodityService;
import com.qdlc.p2p.dal.common.model.jpa.PageDataList;
import com.qdlc.p2p.dal.dao.AdditionalCommodityDao;
import com.qdlc.p2p.dal.dto.AdditionalCommodity;
import com.qdlc.p2p.dal.model.AdditionalCommodityModel;

/**
 * Created by liaoxiangkai on 2015/12/9.
 */
@Service("additionalCommodityService")
public class AdditionalCommodityServiceImpl implements AdditionalCommodityService {

    @Resource
    private AdditionalCommodityDao additionalCommodityDao;

    /**
     * 通过id查询
     * @param id
     * @return
     */
    @Override
    public AdditionalCommodityModel findById(long id) {
        AdditionalCommodity additionalCommodity = additionalCommodityDao.find(id);
        AdditionalCommodityModel additionalCommodityModel = AdditionalCommodityModel.instance(additionalCommodity);
        if (null == additionalCommodity || additionalCommodity.getIsDelete() == AdditionalCommodityModel.IS_DELETE_YES) return null;
        if (null != additionalCommodity.getModifier())
            additionalCommodityModel.setModifierName(additionalCommodity.getModifier().getUserName());
        additionalCommodityModel.setCreatorName(additionalCommodity.getCreator().getUserName());
        return additionalCommodityModel;
    }

    /**
     * 查询商品列表
     * @param model
     * @return
     */
    @Override
    public PageDataList<AdditionalCommodityModel> list(AdditionalCommodityModel model) {
        return additionalCommodityDao.list(model);
    }

    /**
     * 保存商品
     * @param model
     * @return
     */
    @Override
    public AdditionalCommodity save(AdditionalCommodityModel model) {
        return additionalCommodityDao.save(AdditionalCommodityModel.prototype(model));
    }

    /**
     * 更新商品
     * @param model
     * @return
     */
    @Override
    public AdditionalCommodity update(AdditionalCommodityModel model) {
        AdditionalCommodity additionalCommodity = AdditionalCommodityModel.prototype(model);
        AdditionalCommodity temp = additionalCommodityDao.find(model.getId());
        if (null == temp || temp.getIsDelete() == AdditionalCommodityModel.IS_DELETE_YES) return null;
        additionalCommodity.setCreator(temp.getCreator());
        additionalCommodity.setCreateTime(temp.getCreateTime());
        return additionalCommodityDao.update(additionalCommodity);
    }

    /**
     * 启用/禁用
     * @param model
     * @return
     */
    @Override
    public int toggleStatus(AdditionalCommodityModel model) {
        return additionalCommodityDao.toggleStatus(model);
    }

    /**
     * 删除商品
     * @param model
     * @return
     */
    @Override
    public int delete(AdditionalCommodityModel model) {
        return additionalCommodityDao.doDelete(model);
    }

    /**
     * 商品列表json
     * @param category
     * @return
     */
    @Override
    public String lotteryCommodityList(int category) {
        List<AdditionalCommodity> list = additionalCommodityDao.lotteryCommodityList(category);
        JSONArray jsonArray = new JSONArray();//new一个json数组
        if (null != list && list.size() > 0) {
            for (AdditionalCommodity entity : list) {
                JSONObject obj = new JSONObject();
                obj.put("ruleId", entity.getId());
                obj.put("ruleName", entity.getName());
                jsonArray.add(obj);//循环new jsonObject
            }
            //return jsonArray.toString();
        }
        //return "";
        return jsonArray.toString();
    }
}
